package com.ruoyi.admin.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.admin.domain.Experimentrequest;
import com.ruoyi.admin.domain.Semester;
import com.ruoyi.admin.domain.vo.ExperimentrequestVo;
import com.ruoyi.admin.domain.vo.LabscheduleVo;
import com.ruoyi.admin.domain.vo.TLaboratoryVo;
import com.ruoyi.admin.service.IExperimentrequestService;
import com.ruoyi.admin.service.ISemesterService;
import com.ruoyi.admin.service.ITLaboratoryService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.admin.domain.Labschedule;
import com.ruoyi.admin.service.ILabscheduleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实验课安排Controller
 * 
 * @author lin
 * @date 2024-04-28
 */
@RestController
@RequestMapping("/lab/labschedule")
public class LabscheduleController extends BaseController
{
    @Autowired
    private ILabscheduleService labscheduleService;

    @GetMapping(value = "/get/{requestId}")
    public AjaxResult getValueSchedule(@PathVariable("requestId")Long requestId)
    {
        return success(labscheduleService.schedule(requestId));
    }

    @GetMapping(value = "/schedule/{lab}/{requestId}")
    public AjaxResult doSchedule(@PathVariable("lab")Long labId,@PathVariable("requestId")Long requestId)
    {
        if(labscheduleService.doSchedule(labId,requestId)) {
            return success();
        } else {
            return error();
        }

    }


    @Autowired
    private ISemesterService semesterService;

    @Autowired
    private ITLaboratoryService itLaboratoryService;

    @Autowired
    private ISysUserService sysUserService;

    @Resource
    private IExperimentrequestService experimentrequestService;

    /**
     * 查询实验课安排列表
     */
    @PreAuthorize("@ss.hasPermi('lab:labschedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(Experimentrequest experimentrequest)
    {
        startPage();
        List<ExperimentrequestVo> list = experimentrequestService.selectExperimentrequestList(experimentrequest);
        return getDataTable(list);
    }

    /**
     * 导出实验课安排列表
     */
    @PreAuthorize("@ss.hasPermi('lab:labschedule:export')")
    @Log(title = "实验课安排", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Labschedule labschedule)
    {
        List<LabscheduleVo> list = labscheduleService.selectLabscheduleList(labschedule);
        ExcelUtil<LabscheduleVo> util = new ExcelUtil<LabscheduleVo>(LabscheduleVo.class);
        util.exportExcel(response, list, "实验课安排数据");
    }

    /**
     * 获取实验课安排详细信息
     */
    @PreAuthorize("@ss.hasPermi('lab:labschedule:query')")
    @GetMapping(value = "/{scheduleId}")
    public AjaxResult getInfo(@PathVariable("scheduleId") Long scheduleId)
    {
        return success(labscheduleService.selectLabscheduleByScheduleId(scheduleId));
    }

    /**
     * 新增实验课安排
     */
    @PreAuthorize("@ss.hasPermi('lab:labschedule:add')")
    @Log(title = "实验课安排", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Labschedule labschedule)
    {
        TLaboratoryVo tLaboratoryVo = itLaboratoryService.selectTLaboratoryByLabId(labschedule.getLabId());
        if (tLaboratoryVo==null){
            return error("新增实验排课失败，没有相应的实验室");
        }
        Semester semester = semesterService.selectSemesterBySemesterId(labschedule.getSemesterId());
        if (semester==null){
            return error("新增实验排课失败，没有相应的学期");
        }
        return toAjax(labscheduleService.insertLabschedule(labschedule));
    }

    /**
     * 修改实验课安排
     */
    @PreAuthorize("@ss.hasPermi('lab:labschedule:edit')")
    @Log(title = "实验课安排", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Labschedule labschedule)
    {
        return toAjax(labscheduleService.updateLabschedule(labschedule));
    }

    /**
     * 删除实验课安排
     */
    @PreAuthorize("@ss.hasPermi('lab:labschedule:remove')")
    @Log(title = "实验课安排", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scheduleIds}")
    public AjaxResult remove(@PathVariable Long[] scheduleIds)
    {
        return toAjax(labscheduleService.deleteLabscheduleByScheduleIds(scheduleIds));
    }
}
